
const { parse } = require("@vue/compiler-sfc");
const path = require("path");
const { store } = require("../store");

module.exports = function(source){

    const {
        sourceMap,
        resourcePath,
        resourceQuery
    } = this;

    const filename = resourcePath.replace(/\?.*$/, '')

    const { descriptor } = parse(source, {
        filename,
        sourceMap,
    })
    
    if(resourceQuery.includes("type=style")){ // 处理样式

        if(!Array.isArray(descriptor.styles) || descriptor.styles.length == 0){
                return source;
        }

         if(store && !store.value){
            store.value = new Set();
         }

         descriptor.styles.forEach((item)=>{
            const reg = /background-image\s*:\s*url\((['"])?([^'"]+)\?_sprite(['"])?\)/g;
            let result;
            while(result = reg.exec(item.content)){
                const url = result[2]; // 获取到图片名
                const new_url = path.basename(url.trim());
                store.value.add(new_url);
            }   
         })

       return source;

    }else {
        return source;
    }
}

